/**
 * @method Drop 页底解决方案
 * @author Lkx
 * @for Kxui
 * @for method
 *
 * @method use 开始使用
 */

(function (factory) {

  if ((typeof exports === 'object') && (typeof module !== 'undefined')) {
    module.exports = factory();
  } else {
    window.Kxui.drop = factory();
  }

}(function () {

  'use strict';

  let Kxui = window.Kxui;

  /**
   * 逻辑层
   * @method Logic
   * @for Drop
   * @param {object} parameter 配置参数
   */
  let Logic = function (parameter) {
    this.parameter = (typeof parameter === 'object') ? parameter : {};
    this.init();
  };

  Logic.prototype = {

    /**
     * 初始化
     * @method init
     * @for Logic
     */
    init: function () {
      this.el = Kxui.method.getDom(this.parameter.el);
      if (!this.parameter.el) {
        throws(0, 0);
      } else if (!this.el) {
        throws(1, 1, this.parameter.el);
      } else {
        this.elChildren = Kxui.method.sonDom(this.el);
        if (!this.elChildren || this.elChildren.length !== 1) {
          throws(1, 2, this.parameter.el);
        } else {
          this.variable();
        }
      }
    },

    /**
     * 变量生成
     * @method variable
     * @for init
     */
    variable: function () {
      this.timer = '';
      this.leave = (typeof this.parameter.leave === 'function') ? this.parameter.leave : function () {};
      this.callback = (typeof this.parameter.callback === 'function') ? this.parameter.callback : function () {};
      this.time = +this.parameter.time || 100;
      this.deviation = +this.parameter.deviation || 0;
      this.dropSwitch = true;
      this.event();
    },

    /**
     * 事件绑定
     * @method event
     * @for variable
     */
    event: function () {
      let that = this;

      // 监听盒子的滚动情况
      that.el.addEventListener('scroll', onScroll);

      /**
       * 监听盒子的滚动情况
       * @method onScroll
       * @for event
       */
      function onScroll() {
        that.elTop = that.el.scrollTop;
        that.elHeight = that.el.offsetHeight;
        that.elChildrenHeight = that.elChildren[0].offsetHeight;

        let value = that.elTop + that.elHeight + that.deviation;
        if ((value >= that.elChildrenHeight) && that.dropSwitch) {
          clearTimeout(that.timer);
          that.dropSwitch = false;
          that.timer = setTimeout(function () {
            that.callback();
          }, that.time);
        } else if ((value < that.elChildrenHeight) && !that.dropSwitch) {
          clearTimeout(that.timer);
          that.dropSwitch = true;
          that.timer = setTimeout(function () {
            that.leave();
          }, that.time);
        }
      }
    }
  };

  /**
   * 控制台错误/警告
   * @method throws
   * @for variable
   * @param {number} error 抛出错误的类型 0:警告 1:错误
   * @param {number} num 输入警告文案编号
   * @param {string} dome 发生错误的节点
   */
  function throws(error, num, dome) {
    if (Kxui.configuration.debug) {
      let nums = {};
      nums[0] = '配置参数 {el} 不能为空';
      nums[1] = '无法找到 {' + dome + '} 节点或存在多个 {' + dome + '} 节点';
      nums[2] = '无法找到 {' + dome + '} 子节点或存在多个子节点';
      let text = 'Kxui-' + Kxui.about.version + '： 模块 {drop} ' + nums[num] + '，请检查修改。';
      error === 1 ? console.error(text) : console.warn(text);
    }
  }

  /**
   * 页底解决方案
   * @method Drop
   */
  let Drop = function () {
    this.name = 'Drop';
    this.info = 'Page bottom solution';
  };

  Drop.fn = Drop.prototype;

  /**
   * 开始使用
   * @method use
   * @for Drop
   * @param {object} parameter 配置参数
   */
  Drop.fn.use = function (parameter) {
    new Logic(parameter);
  };

  return new Drop();

}));